//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using System.Xml.XPath;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Installer.Packages;
namespace DotNetNuke.Services.Installer.Writers
{
	public class SkinControlPackageWriter : PackageWriterBase
	{
		private SkinControlInfo _SkinControl;
		public SkinControlPackageWriter(PackageInfo package)
			: base(package)
		{
			_SkinControl = SkinControlController.GetSkinControlByPackageID(package.PackageID);
			BasePath = Path.Combine("DesktopModules", package.Name.ToLower()).Replace("/", "\\");
			AppCodePath = Path.Combine("App_Code", package.Name.ToLower()).Replace("/", "\\");
		}
		public SkinControlPackageWriter(SkinControlInfo skinControl, PackageInfo package)
			: base(package)
		{
			_SkinControl = skinControl;
			BasePath = Path.Combine("DesktopModules", package.Name.ToLower()).Replace("/", "\\");
			AppCodePath = Path.Combine("App_Code", package.Name.ToLower()).Replace("/", "\\");
		}
		public SkinControlPackageWriter(XPathNavigator manifestNav, InstallerInfo installer)
		{
			_SkinControl = new SkinControlInfo();
			Package = new PackageInfo(installer);
			ReadLegacyManifest(manifestNav, true);
			Package.Description = Null.NullString;
			Package.Version = new Version(1, 0, 0);
			Package.PackageType = "SkinObject";
			Package.License = Util.PACKAGE_NoLicense;
			BasePath = Path.Combine("DesktopModules", Package.Name.ToLower()).Replace("/", "\\");
			AppCodePath = Path.Combine("App_Code", Package.Name.ToLower()).Replace("/", "\\");
		}
		public SkinControlInfo SkinControl {
			get { return _SkinControl; }
			set { _SkinControl = value; }
		}
		private void ReadLegacyManifest(XPathNavigator legacyManifest, bool processModule)
		{
			XPathNavigator folderNav = legacyManifest.SelectSingleNode("folders/folder");
			if (processModule) {
				Package.Name = Util.ReadElement(folderNav, "name");
				Package.FriendlyName = Package.Name;
				foreach (XPathNavigator controlNav in folderNav.Select("modules/module/controls/control")) {
					SkinControl.ControlKey = Util.ReadElement(controlNav, "key");
					SkinControl.ControlSrc = Path.Combine(Path.Combine("DesktopModules", Package.Name.ToLower()), Util.ReadElement(controlNav, "src")).Replace("\\", "/");
					string supportsPartialRendering = Util.ReadElement(controlNav, "supportspartialrendering");
					if (!string.IsNullOrEmpty(supportsPartialRendering)) {
						SkinControl.SupportsPartialRendering = bool.Parse(supportsPartialRendering);
					}
				}
			}
			foreach (XPathNavigator fileNav in folderNav.Select("files/file")) {
				string fileName = Util.ReadElement(fileNav, "name");
				string filePath = Util.ReadElement(fileNav, "path");
				AddFile(Path.Combine(filePath, fileName), fileName);
			}
			if (!string.IsNullOrEmpty(Util.ReadElement(folderNav, "resourcefile"))) {
				AddFile(Util.ReadElement(folderNav, "resourcefile"));
			}
		}
		protected override void WriteManifestComponent(System.Xml.XmlWriter writer)
		{
			writer.WriteStartElement("component");
			writer.WriteAttributeString("type", "SkinObject");
			CBO.SerializeObject(SkinControl, writer);
			writer.WriteEndElement();
		}
	}
}
